<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.tree-container{max-width: 100%;max-height: 100%}
	.checkbox-item{margin-right: 15px;position: relative;top: 8px}
	.tree .tree-item{max-width: 100%;margin-right: 19px}
	.grade-edit, .grade-delete, .grade-add-sub{float: right;margin-left: 15px}
	#new-section{margin-bottom: 10px}
	
</style>

<div class="page-header">
	<h1>资源年级管理</h1>
</div>
<div class="col-xs-12">
	<button class="btn btn-primary" id="new-section">
		<i class="ace-icon fa fa-plus"></i> 添加学段
	</button>
</div>
<div class="col-xs-12 col-sm-5">
	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> 资源年级预览
		</h4>

		<div class="tree-container">
			<ul id="cat-tree"></ul>
		</div>
	</div>
</div>

<div class="col-xs-12 col-sm-7 hidden" id="section-form-div">
	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> 学段编辑
		</h4>
		<form class="form-horizontal" role="form" id="section-form">
			<input type="hidden" name="id" value="" />
			<input type="hidden" name="pid" value="0" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 学段名称：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="title" placeholder="请填写学段名称，比如 学前，小学，初中..."/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 学段排序：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="grade_sort" placeholder="请填写学段排序，从高往低排序" value="0" />
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
</div>
<div class="col-xs-12 col-sm-7 hidden" id="grade-form-div">
	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> 年级编辑
		</h4>
		<form class="form-horizontal" role="form" id="grade-form">
			<input type="hidden" name="id" value="" />
			<input type="hidden" name="pid" value="" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 所属学段：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" disabled ="" id="parent_grade" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 年级名称：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="title" placeholder="请填写年级名称"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 年级排序：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="grade_sort" placeholder="请填写年级排序，从高往低排序" value="0" />
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
</div>
		
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 资源年级
		var gradeSource = function(options, callback){
			var $data = null
			if(!("text" in options) && !("type" in options)){
				$data = <?= $gradeStr ?>;//the root tree
				callback({ data: $data });
				return;
			} else if("type" in options && options.type == "folder") {
				if("additionalParameters" in options && "children" in options.additionalParameters)
					$data = options.additionalParameters.children || {};
				else $data = {}//no data
			}
			callback({ data: $data })
		}

		$('#cat-tree').ace_tree({
			dataSource: gradeSource,
			'itemSelect' : false,
			'folderSelect': true,
		}).tree("discloseVisible");
		
		// 学段表单
		var sectionForm = $("#section-form");
		// 年级表单
		var gradeForm = $("#grade-form");
		var gradeSubmitUrl = "<?=admin_url("admin/save_source_grade_action")?>";

		// 新增学段
		$("#new-section").click(function(){
			sectionForm[0].reset();
			sectionForm.find("[name=id]").val(0);

			// 显示
			$("#section-form-div").removeClass("hidden");
			$("#grade-form-div").addClass("hidden");
		})

		// 新增年级
		$("body").on("click", ".grade-add-sub", function(){
			var id = $(this).closest("li").attr("id");
			gradeForm[0].reset();
			gradeForm.find("[name=id]").val(0);
			gradeForm.find("[name=pid]").val(id);
			gradeForm.find("#parent_grade").val($(this).prevAll("span").text());

			// 显示
			$("#grade-form-div").removeClass("hidden");
			$("#section-form-div").addClass("hidden");
		})

		// 编辑学段和年级
		$("body").on("click", ".grade-edit", function(){
			var id = $(this).closest("li").attr("id");
			// 获取年级
			var getUrl = "<?=admin_url('admin/get_source_grade_info')?>";
			var getData = {"id": id};
			ajax_get(getUrl, getData, function(res){
				var grade = res.data; console.log(grade.grade_pid);
				// 编辑学段
				if(0 == grade.grade_pid){
					sectionForm.find("[name=id]").val(id);
					sectionForm.find("[name=grade_pid]").val(0);
					sectionForm.find("[name=title]").val(grade.title);
					sectionForm.find("[name=grade_sort]").val(grade.grade_sort);

					// 显示
					$("#section-form-div").removeClass("hidden");	
					$("#grade-form-div").addClass("hidden");
				} else {
					// 编辑年级
					gradeForm.find("[name=id]").val(id);
					gradeForm.find("[name=grade_pid]").val(grade.grade_pid);
					gradeForm.find("[name=title]").val(grade.title);
					gradeForm.find("[name=grade_sort]").val(grade.grade_sort);
					// 设置所属学段名称
					gradeForm.find("#parent_grade").val(grade.parent_title);
					// 显示
					$("#section-form-div").addClass("hidden");
					$("#grade-form-div").removeClass("hidden");
				}
			})
		})

		// 删除学段或年级
		$("body").on("click", ".grade-delete", function(e){
			var id = $(this).closest("li").attr("id");
			var title = $(this).closest("span").children("span").text();
			layer.confirm('确定删除' + title + '吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('admin/delete_source_grade_action')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				})
			});
		})

		// 提交学段表单
		form_submit(sectionForm, gradeSubmitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
		// 提交年级表单
		form_submit(gradeForm, gradeSubmitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
	})
</script>